package cn.itcast.mq.listeners;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.security.PublicKey;
import java.time.LocalDateTime;
import java.util.Map;

@Component
public class MQListeners {

    @RabbitListener(queues = "simple1.queue")
    public void listenerSimpleQueueMessage(String msg) {

        System.out.println("消费者收到的msg内容：： = " + msg);
    }

    //WorkQueue队列  生产者发送消息给一个队列 一个队列绑定多个消费者
    @RabbitListener(queues = "simpleWork.queue")
    public void listenerWorkQueueMessage1(String msg) throws InterruptedException {

        System.out.println("消费者1111111111111111收到的msg内容：： = " + msg + "   " + LocalDateTime.now());
        Thread.sleep(100);
    }

    @RabbitListener(queues = "simpleWork.queue")
    public void listenerWorkQueueMessage2(String msg) throws InterruptedException {

        System.out.println("消费者222222收到的msg内容：： = " + msg + "   " + LocalDateTime.now());

        Thread.sleep(25);
    }

    //Fanout Exchange  通过交换机将消息广播到绑定的队列queue中  队列绑定消费者
    @RabbitListener(queues = "fanout.queue1")
    public void listenerFanoutExchangeQueue1Message(String message) {
        System.out.println("FanoutExchangeQueue1111111111收到的message = " + message);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenerFanoutExchangeQueue2Message(String message) {
        System.out.println("FanoutExchangeQueue2222222222收到的message = " + message);
    }

    //Direct Exchange 消息或通过路由的方式分配给对应的队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.directExchange", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenerDirectExchange1QueueMessage(String message) {
        System.out.println("DirectExchangeQueue11111收到的message = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.directExchange"),
            key = {"blue", "yellow"}
    ))
    public void listenerDirectExchangeQueue2Message(String message) {
        System.out.println("DirectExchangeQueue22222收到的message = " + message);
    }


    //    Topic Exchange 通过RoutingKey进行路由分配，可以使多个单词列表，使用" ."进行分割
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topicExchange", type = ExchangeTypes.TOPIC),
            key = "河南.#"
    ))
    public void listenersTopicExchangeQueue1Message(String message) {
        System.out.println("TopicExchangeQueue1(河南.#)收到的消息是message = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topicExchange", type = ExchangeTypes.TOPIC),
            key = "河北.#"
    ))
    public void listenerTopicExchangeQueue2Message(String message) {
        System.out.println("TopicExchangeQueue2(河北.#)收到的消息是message = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue3"),
            exchange = @Exchange(name = "itcast.topicExchange", type = ExchangeTypes.TOPIC),
            key = "#.美女"
    ))
    public void listenerTopicExchangeQueue3Message(String message) {
        System.out.println("TopicExchangeQueue3(#.美女)收到的消息是message = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue4"),
            exchange = @Exchange(name = "itcast.topicExchange", type = ExchangeTypes.TOPIC),
            key = "#.帅哥"
    ))
    public void listenerTopicExchangeQueue4Message(String message) {
        System.out.println("TopicExchangeQueue4(#.帅哥)收到的消息是message = " + message);
    }


//    消息中发送任何类型的数据均转为json
    @RabbitListener(queues = "object.queue")
    public void listenerObjectQueue(Object o){
        System.out.println("发送的消息是object类型的数据= " + o);
    }

    @RabbitListener(queues = "object.queue")
    public void listenerObjectQueue1(Map<String,Object> map){
        System.out.println("发送的消息是map类型的数据= " + map);
    }

}
